/**
 * swappSpyConfig.js
 * Used by the dialog box for the settings of the extension
 * @author Chen Wang & Clément Grimal
 */

/**
 * swappSpyConfig
 * Used by the dialog box for the settings of the extension
 * @type 
 */
var swappSpyConfig = {
	MacOS : false,
	
	updateProxySettings: function() {
		var proxyHost = window.document.getElementById("swappspy-proxy-host").value;
		var proxyPort = window.document.getElementById("swappspy-proxy-port").value;
		
		var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
		prefs = prefs.getBranch("extensions.swappspy.");
		prefs.setCharPref("http", proxyHost);
		prefs.setIntPref("http_port", proxyPort);
		
		if(prefs.getBoolPref("active")){
			var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
			prefs = prefs.getBranch("network.proxy.");
			prefs.setCharPref("http", proxyHost);
			prefs.setIntPref("http_port", proxyPort);
		}
		
		if(swappSpyConfig.MacOS) {
			window.close();
		}
	},
	addMacButtons : function() {
		if(window.navigator.appVersion.indexOf("Mac") != -1) swappSpyConfig.MacOS = true;  //indexOf("Win")
		if(swappSpyConfig.MacOS) {
			var containerNode = window.document.getElementById("buttons-box");
			var acceptButton = window.document.createElement("button");
			var cancelButton = window.document.createElement("button");
			
			var acceptLabel = "Undefined";
			var cancelLabel = "Undefined";
			try {
				var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
							.getService(Components.interfaces.nsIStringBundleService)
     			src = 'chrome://swappSpy/locale/swappSpy.properties';
     			var bundle = stringBundleService.createBundle(src)

				acceptLabel = bundle.GetStringFromName('config.button.accept.label');
				cancelLabel = bundle.GetStringFromName('config.button.cancel.label');
			} catch(e) {
				alert("Cannot get string from bundle file.");
				alert(e);
			}
			
			acceptButton.setAttribute("label", acceptLabel);
			acceptButton.setAttribute("oncommand", "javascript:swappSpyConfig.updateProxySettings();");
			containerNode.appendChild(acceptButton);
			
			cancelButton.setAttribute("label", cancelLabel);
			cancelButton.setAttribute("oncommand", "javascript:window.close();");
			containerNode.appendChild(cancelButton);
		}
	}
}